import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { WsAdapter } from './adapters/ws-adapter';
import { join } from 'path';
import { NestExpressApplication } from '@nestjs/platform-express';
import { HttpExceptionFilter } from './filters/http.exception.filter';
import { Logger } from '@nestjs/common';
import * as exphbs from 'express-handlebars';

var hbs = require('hbs');

async function bootstrap() {
  // const app = await NestFactory.create(AppModule);
  // app.useWebSocketAdapter(new WsAdapter(app));
  const app = await NestFactory.create<NestExpressApplication>(
    AppModule,
  );
  
  app.useStaticAssets(join(__dirname, '..', 'public'));
  app.setBaseViewsDir(join(__dirname, '..', 'views'));

  // const helpers = {
  //   hlp: (echo) => `Echo: ${echo}.`,
  //   toJSON: (object) => JSON.stringify(object),
  // };
  // const hbs = exphbs.create({
  //   extname: '.hbs',
  //   defaultLayout: false,
  //   layoutsDir: join(__dirname, 'views', 'layouts'),
  //   helpers,
  // });

  // app.engine('.hbs', hbs.engine);
  // app.setViewEngine('.hbs');

  app.setViewEngine('hbs');
  hbs.registerHelper('toJSON', function (object) { return JSON.stringify(object); })

  const logger: Logger = new Logger();
  app.useGlobalFilters(new HttpExceptionFilter(logger));

  const PORT = process.env.PORT || 3000;
  logger.log("Nest application listent on " + PORT, "Main")
  await app.listen(PORT);
}
bootstrap();
